package com.example.devices

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.app.path.RouterPath
import com.example.common.view.view.BaseMvvmActivity
import com.example.device.R
import com.example.device.databinding.ActivityOrderBinding
import com.example.devices.adapter.OrderAdapter
import com.example.devices.vm.DevicesViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RouterPath.PATH_DEVICES_ORDER)
@AndroidEntryPoint
class OrderActivity : BaseMvvmActivity<ActivityOrderBinding>() {
    val orderAdapter = OrderAdapter()
    private val orderViewModel:DevicesViewModel by viewModels()
    override fun initView() {
        mDataBinding.rvOrder.adapter = orderAdapter
        mDataBinding.rvOrder.layoutManager = LinearLayoutManager(this)
        orderAdapter.setOnItemClickListener { adapter, view, position ->
            val id = orderAdapter.data.get(position).id
            ARouter.getInstance().build(RouterPath.PATH_ORDER_DETAIL)
                .withString("id",id.toString())
                .navigation()
        }
        mDataBinding.back.setOnClickListener {
            finish()
        }
    }

    override fun loadData() {
        orderViewModel.orderData()
    }

    override fun observeDataChange() {
        orderViewModel.mOrderSuccess.observe(this){
            ToastUtils.showShort("请求列表成功")
            orderAdapter.data.addAll(it)
            orderAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_order

}